<template>
  <div class="app-container">
    <el-table
      ref="dragTable"
      :data="columns"
      row-key="columnId"
      :max-height="tableHeight"
    >
      <el-table-column
        label="序号"
        type="index"
        min-width="50%"
        class-name="allowDrag"
      />
      <el-table-column
        label="字段描述"
        min-width="10%"
        prop="comment"
        class-name="allowDrag"
      />
      <el-table-column
        label="字段列名"
        prop="field_name"
        min-width="10%"
        :show-overflow-tooltip="true"
        class-name="allowDrag"
      />
    </el-table>
    <el-form label-width="100px">
      <el-form-item
        style="text-align: center; margin-left: -100px; margin-top: 10px"
      >
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template> 
<script>
import axios from "axios";
import { deleteoperation, bitableoperation } from "@/api/conf/bitableoperation";
import Sortable from "sortablejs";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
  props: {
    tableName: String,
    default: "",
  },
  data() {
    return {
      // 选中选项卡的 name
      activeName: "columnInfo",
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 表信息
      tables: [],
      // 表列信息
      columns: [],
      // 字典信息
      dictOptions: [],
      // 菜单信息
      menus: [],
      // 表详细信息
      info: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      let obj = {
        params: {
          tableName: this.tableName,
          page: 1,
          size: 10,
        },
        data: {},
      };
      bitableoperation(obj).then((response) => {
        this.columns = response.data.cols ? response.data.cols : [];
        this.columns.forEach((item, index) => {
          item.columnId = index.toString();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.handleadjustFieldPosition();
    },
    handleadjustFieldPosition() {
      let fd = [...this.columns];
      // fd.tableTotal = this.cols.map((item) => {
      //   item.field_name, item.comment;
      // });
      axios
        .put(
          process.env.VUE_APP_BASE_API +
            "/conf/bitableoperation/adjustFieldPosition?tableName=" +
            this.tableName,
          fd,
          {
            headers: {
              header: {
                // responseType: "blob"
              },

              Authorization: "Bearer " + getToken(),
              // "Content-Type": "multipart/form-data",
            }, //定义内容格式,很重要
            // timeout: 20000,
          }
        )
        .then((response) => {
          if (response.data.code == 200) {
            this.$modal.msgSuccess("调整成功");
          } else {
            this.$modal.msgError("调整失败");
          }
        });
    },
    /** 关闭按钮 */
    close() {
      this.$emit("close");
    },
  },
  mounted() {
    const el = this.$refs.dragTable.$el.querySelectorAll(
      ".el-table__body-wrapper > table > tbody"
    )[0];
    const sortable = Sortable.create(el, {
      handle: ".allowDrag",
      onEnd: (evt) => {
        const targetRow = this.columns.splice(evt.oldIndex, 1)[0];
        this.columns.splice(evt.newIndex, 0, targetRow);
        for (let index in this.columns) {
          this.columns[index].sort = parseInt(index) + 1;
        }
      },
    });
    console.log(JSON.stringify(this.columns));
  },
};
</script>